
<?php
import("@.Action.CommonAction");
class ImportSheetAction extends Common {
    public function index(){
		$this->display("ViewParentsProduct");
    }

	 public function Edit(){
		$Model = new Model();
		$ImportProductSheetID = $_GET['ImportProductSheetID'];
		$ImportProductSheetList = "";
		$SizeNameList = null;
		$ColorNameList = null;

		if($ImportProductSheetID != "")
		{
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   * ");
			$sqlB->Append("FROM  ImportProductSheet ");
			$sqlB->Append("WHERE ImportProductSheet.ID = " . $ImportProductSheetID . " ");
			$sqlB->Append("LIMIT 1");
			$sqlB->Append(";");
			$ImportProductSheetList = $Model->query($sqlB->ToString());
			$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_en'], "", $ImportProductSheetList[0]['item_name']));
			$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_zh'], "", $ImportProductSheetList[0]['item_name']));


			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ProductSubTypes.ProductSubType ");
			$sqlB->Append(" , ProductSubTypes.Code AS Code1 ");
			$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
			$sqlB->Append(" , SecondProductSubTypes.Code AS Code2 ");
			$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
			$sqlB->Append(" , ThirdProductSubTypes.Code AS Code3  ");
			$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
			$sqlB->Append(" , FourthProductSubTypes.Code AS Code4 ");
			$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
			$sqlB->Append(" , FifthProductSubTypes.Code AS Code5  ");
			$sqlB->Append("FROM ProductSubTypes ");
			$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");
			$sqlB->Append("WHERE ");
			$sqlB->Append("ProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
			$sqlB->Append("OR SecondProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
			$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
			$sqlB->Append("OR FourthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
			$sqlB->Append("OR FifthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
			$sqlB->Append("LIMIT 1 ");
			$sqlB->Append(";");

			$ProductSubTypesList = $Model->query($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ID ");
			$sqlB->Append(" , ParentID ");
			$sqlB->Append(" , size_name ");
			$sqlB->Append("FROM  ImportProductTransSheet ");
			$sqlB->Append("WHERE ImportProductTransSheet.ParentID = " . $ImportProductSheetID . " ");
			$sqlB->Append("GROUP BY size_name ");
			$sqlB->Append(";");
			$SizeNameList = $Model->query($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ID ");
			$sqlB->Append(" , ParentID ");
			$sqlB->Append(" , color_name ");
			//$sqlB->Append(" , color_map ");
			$sqlB->Append("FROM  ImportProductTransSheet ");
			$sqlB->Append("WHERE ImportProductTransSheet.ParentID = " . $ImportProductSheetID . " ");
			$sqlB->Append("GROUP BY color_name ");
			$sqlB->Append(";");
			$ColorNameList = $Model->query($sqlB->ToString());
			
		}

		$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes", "ProductSubtype = '鞋靴'");
		$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->assign('ImportProductSheetList',$ImportProductSheetList);
		$this->assign('SizeNameList',$SizeNameList);
		$this->assign('ColorNameList',$ColorNameList);
		$this->assign('ProductSubTypesList',$ProductSubTypesList);

		if($ImportProductSheetID != ""){
			$this->display("EditImportSheet");
		}
		else{
			$this->display("CreateImportSheet");
		}
		
	 }

	 public function Deleted(){
		$ImportSheetID = $_GET['ImportSheetID'];
		$ImportProductSheetID =  $_GET['ImportProductSheetID'];

		$Model = new Model();

		/*$sqlB = new StringBuilder();
		$sqlB->Append("DELETE FROM ");
		$sqlB->Append("   ImportSheet ");
		$sqlB->Append("WHERE ImportSheetID = " . $ImportSheetID);
		$sqlB->Append(";");
		
		$result	= $Model->execute($sqlB->ToString());*/

		$sqlB = new StringBuilder();
		$sqlB->Append("DELETE FROM ");
		$sqlB->Append("   ImportProductSheet ");
		$sqlB->Append("WHERE ID = " . $ImportProductSheetID);
		$sqlB->Append(";");
		$result = $Model->execute($sqlB->ToString());

		
		$sqlB = new StringBuilder();
		$sqlB->Append("DELETE FROM ");
		$sqlB->Append("   ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = " . $ImportProductSheetID);
		$sqlB->Append(";");

		$result = $Model->execute($sqlB->ToString());

		$this->redirect("/Index");
	 }


	 public function ViewParentsProduct(){
	 
		$Model = new Model();

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportSheet.ImportSheetID ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("WHERE DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d')= '". date("Y-m-d", time()). "' ");
		$sqlB->Append("AND UserID = " . $_SESSION['UserID']);
		$sqlB->Append(";");

		$ImportSheetID = $Model->query($sqlB->ToString());

		if(!$ImportSheetID)
		{
			$sqlB = new StringBuilder();
			$sqlB->Append("INSERT INTO ");
			$sqlB->Append("   ImportSheet ( ");
			$sqlB->Append("   UserID ");
			$sqlB->Append(" , `Date` ");
			$sqlB->Append(" , TimeUpdated ");
			$sqlB->Append(" , UpdatedBy ");
			$sqlB->Append(" , TimeCreated ");
			$sqlB->Append(" , CreatedBy ");
			$sqlB->Append("   ) ");
			$sqlB->Append("SELECT ");
			$sqlB->Append("   " . $_SESSION['UserID'] . " AS UserID ");
			$sqlB->Append(" , NOW() AS `Date` ");
			$sqlB->Append(" , NOW() AS TimeUpdated ");
			$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
			$sqlB->Append(" , NOW() AS TimeCreated ");
			$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
			
		}

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   TableFields.Field ");
		$sqlB->Append(" , TableFields.Alias ");
		$sqlB->Append(" , TableFields.RefTable ");
		$sqlB->Append(" , TableFields.RefField ");
		$sqlB->Append(" , TableFields.RefFieldDisplay ");
		$sqlB->Append("FROM Tables ");
		$sqlB->Append("INNER JOIN TableFields ON `Tables`.Table = TableFields.Table ");
		$sqlB->Append("   AND `Tables`.`Table` = 'ImportProductSheet' ");
		$sqlB->Append("   AND `TableFields`.`DisplayInParent` = 1 ");
		$sqlB->Append("ORDER BY TableFields.`Sequence` ");
		$sqlB->Append(";");

		$TableFieldsInfo = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   `Tables`.`Key` ");
		$sqlB->Append(" , `Tables`.Alias ");
		$sqlB->Append(" , `Tables`.`Table` ");
		$sqlB->Append("FROM `Tables` ");
		$sqlB->Append("WHERE `Tables`.`Table` = 'ImportProductSheet' ");
		$sqlB->Append("Limit 1");
		$sqlB->Append(";");

		$TableInfo = $Model->query($sqlB->ToString());

		import('ORG.Page');// 导入分页类

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   COUNT(ImportProductSheet.ImportSheetID) AS DataCount1 ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("INNER JOIN Users ON ImportSheet.UserID = Users.UserID ");
		$sqlB->Append("INNER JOIN ImportProductSheet ON ImportSheet.ImportSheetID = ImportProductSheet.ImportSheetID ");
		$sqlB->Append("WHERE Users.UserID = ".$_SESSION['UserID'] . " ");
		$sqlB->Append("UNION ");
		$sqlB->Append("SELECT ");
		$sqlB->Append("   COUNT(ClothingImportSheet.ImportSheetID) AS DataCount1 ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("INNER JOIN Users ON ImportSheet.UserID = Users.UserID ");
		$sqlB->Append("INNER JOIN ClothingImportSheet ON ImportSheet.ImportSheetID = ClothingImportSheet.ImportSheetID ");
		$sqlB->Append("WHERE Users.UserID = ".$_SESSION['UserID'] . " ");

		$DataCount = $Model->query($sqlB->ToString());
		
		$count      = $DataCount[0]['DataCount1'] + $DataCount[1]['DataCount1'];// 查询满足要求的总记录数 $map表示查询条件
		$Page       = new Page($count, 25);// 实例化分页类 传入总记录数
		$show       = $Page->show();// 分页显示输出

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT * FROM (");
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductSheet.ImportSheetID ");
		$sqlB->Append(" , 'Shoes' AS ProductTyle ");
		$sqlB->Append(" , ImportProductSheet.ID ");
		$sqlB->Append(" , DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d') AS `Date` ");
		$sqlB->Append(" , ImportProductSheet.item_sku ");
		$sqlB->Append(" , ImportProductSheet.item_name ");
		$sqlB->Append(" , ImportProductSheet.TimeCreated ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("INNER JOIN Users ON ImportSheet.UserID = Users.UserID ");
		$sqlB->Append("INNER JOIN ImportProductSheet ON ImportSheet.ImportSheetID = ImportProductSheet.ImportSheetID ");
		$sqlB->Append("WHERE Users.UserID = ".$_SESSION['UserID'] . " ");

		$sqlB->Append(" UNION  ALL ");

		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportSheet.ImportSheetID ");
		$sqlB->Append(" , 'Clothing' AS ProductTyle ");
		$sqlB->Append(" , ClothingImportSheet.ID ");
		$sqlB->Append(" , DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d') AS `Date` ");
		$sqlB->Append(" , ClothingImportSheet.item_sku ");
		$sqlB->Append(" , ClothingImportSheet.item_name ");
		$sqlB->Append(" , ClothingImportSheet.TimeCreated ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("INNER JOIN Users ON ImportSheet.UserID = Users.UserID ");
		$sqlB->Append("INNER JOIN ClothingImportSheet ON ImportSheet.ImportSheetID = ClothingImportSheet.ImportSheetID ");
		$sqlB->Append("WHERE Users.UserID = ".$_SESSION['UserID'] . " ");
		$sqlB->Append(" ) AS t ");
		$sqlB->Append("ORDER BY t.TimeCreated DESC ");
		$sqlB->Append("Limit ");

		$sqlB->Append($Page->firstRow.','.$Page->listRows);
		//$sqlB->Append("AND DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d') = '". date("Y-m-d", time()). "'");
		$sqlB->Append(";");

		$TableList = $Model->query($sqlB->ToString());

		$this->assign('page',$show);// 赋值分页输出
		$this->assign('TableInfo',$TableInfo);
		$this->assign('TableFieldsInfo',$TableFieldsInfo);
		$this->assign('TableList',$TableList);
		$this->display("ViewParentsProduct");
	 }

	public function SearchParentsProduct(){
	 
		
		$Product = $_GET['Product'];

		if($Product != ""){
			$Model = new Model();
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   TableFields.Field ");
			$sqlB->Append(" , TableFields.Alias ");
			$sqlB->Append(" , TableFields.RefTable ");
			$sqlB->Append(" , TableFields.RefField ");
			$sqlB->Append(" , TableFields.RefFieldDisplay ");
			$sqlB->Append("FROM Tables ");
			$sqlB->Append("INNER JOIN TableFields ON `Tables`.Table = TableFields.Table ");
			$sqlB->Append("   AND `Tables`.`Table` = 'ImportProductSheet' ");
			$sqlB->Append("   AND `TableFields`.`DisplayInParent` = 1 ");
			$sqlB->Append("ORDER BY TableFields.`Sequence` ");
			$sqlB->Append(";");

			$TableFieldsInfo = $Model->query($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `Tables`.`Key` ");
			$sqlB->Append(" , `Tables`.Alias ");
			$sqlB->Append(" , `Tables`.`Table` ");
			$sqlB->Append("FROM `Tables` ");
			$sqlB->Append("WHERE `Tables`.`Table` = 'ImportProductSheet' ");
			$sqlB->Append("Limit 1");
			$sqlB->Append(";");

			$TableInfo = $Model->query($sqlB->ToString());


			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ImportProductSheet.ImportSheetID ");
			$sqlB->Append(" , ImportProductSheet.ID ");
			$sqlB->Append(" , DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d') AS `Date` ");
			$sqlB->Append(" , ImportProductSheet.item_sku ");
			$sqlB->Append(" , ImportProductSheet.item_name ");
			$sqlB->Append(" , ImportProductSheet.TimeCreated ");
			$sqlB->Append("FROM  ImportSheet ");
			$sqlB->Append("INNER JOIN Users ON ImportSheet.UserID = Users.UserID ");
			$sqlB->Append("INNER JOIN ImportProductSheet ON ImportSheet.ImportSheetID = ImportProductSheet.ImportSheetID ");
			$sqlB->Append("WHERE Users.UserID = ".$_SESSION['UserID'] . " ");
			$sqlB->Append("AND  ImportProductSheet.`item_name` LIKE  '%" . $Product . "%'");
			$sqlB->Append("Limit 100");
			$sqlB->Append(";");

			$TableList = $Model->query($sqlB->ToString());

			$this->assign('TableInfo',$TableInfo);
			$this->assign('TableFieldsInfo',$TableFieldsInfo);
			$this->assign('TableList',$TableList);
		}

		$this->display("ViewParentsProduct");
	 }

	Public function UploadImage(){

		import("ORG.UploadFile");
		$ImportProductSheetID = $_POST['ImportProductSheetID'];
		$ColorName = $_POST['ColorName'];
		$ImageType = $_POST['ImageType'];

		$Model = new Model();
		$jsonencode = "";
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  '../Uploads/'.$_SESSION['UserID']. "/";// 设置附件上传目录
		$upload->uploadReplace = true;
		 $upload->thumb = true;
        // 设置引用图片类库包路径
            $upload->imageClassPath ='ORG.Util.Image';
        //设置需要生成缩略图的文件后缀
            $upload->thumbPrefix='s_';  //生产2张缩略图
        //设置缩略图最大宽度
            $upload->thumbMaxWidth='100';
        //设置缩略图最大高度
            $upload->thumbMaxHeight='100';
        //设置上传文件规则
            $upload->saveRule='uniqid';
        //删除原图
            $upload->thumbRemoveOrigin=false;
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
				//更新ImportProductSheet中的主图和附图
			$info =  $upload->getUploadFileInfo();

			if($ImageType == "main"){
				$sqlB = new StringBuilder();
				$sqlB->Append("UPDATE ImportProductSheet ");
				$sqlB->Append("SET ");
				$sqlB->Append("   main_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"));
				$sqlB->Append(" , swatch_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"). " ");
				$sqlB->Append("WHERE ImportProductSheet.ID = ".$ImportProductSheetID . " ");
				$sqlB->Append("AND ImportProductSheet.main_image_url IS NULL ");
				$sqlB->Append("AND ImportProductSheet.swatch_image_url IS NULL ");
				$sqlB->Append(";");
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("UPDATE ImportProductTransSheet ");
				$sqlB->Append("SET ");
				$sqlB->Append("   main_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"));
				$sqlB->Append(" , swatch_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
				$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
				$sqlB->Append("AND ImportProductTransSheet.color_name = ".FData($ColorName, "Text") . " ");
				$sqlB->Append("AND ImportProductTransSheet.main_image_url IS NULL ");
				$sqlB->Append("AND ImportProductTransSheet.swatch_image_url IS NULL ");
				$sqlB->Append(";");
				$Model->execute($sqlB->ToString());

				$arr = array
					(
						'URLID'=> 'main_image_url',
						'URL'=>'/Uploads/'.$_SESSION['UserID']. "/s_" . $info[0]['savename']
					);

				$jsonencode = json_encode($arr);
			}
			else{ 
				$sqlB = new StringBuilder();
				$sqlB->Append("SELECT ");
				$sqlB->Append("   ImportProductTransSheet.other_image_url1 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url2 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url3 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url4 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url5 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url6 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url7 ");
				$sqlB->Append(" , ImportProductTransSheet.other_image_url8 ");
				$sqlB->Append("FROM ImportProductTransSheet ");
				$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
				$sqlB->Append("AND ImportProductTransSheet.color_name = ".FData($ColorName, "Text") . " ");
				$sqlB->Append("ORDER BY ImportProductTransSheet.ID ");
				$sqlB->Append("LIMIT 1 ");
				$sqlB->Append(";");

				$ImportProductTransSheetList = $Model->query($sqlB->ToString());

				$tempTableName = "tmp_" . date("Ymd_His") . '_' . rand(100,999);
				$sqlB = new StringBuilder();
				$sqlB->Append("CREATE TABLE " . $tempTableName . " ( ");
				$sqlB->Append("   `ID` varchar(50) DEFAULT NULL, ");
				$sqlB->Append("   `Url` varchar(50) DEFAULT NULL ");
				$sqlB->Append(" );");
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("INSERT INTO " . $tempTableName . " values ");

				for($i=1; $i <= 7; $i++){
					$URLValue = "NULL";
					if($ImportProductTransSheetList [0]['other_image_url'.$i] != Null && $ImportProductTransSheetList [0]['other_image_url'.$i] != ""&& $ImportProductTransSheetList [0]['other_image_url'.$i] != "Null"){
						$URLValue = "'". $ImportProductTransSheetList [0]['other_image_url'.$i]. "'";
					}

					$sqlB->Append("('other_image_url" . $i . "', " . $URLValue . "), ");

				}
				//为了最后一次循环 sql 的结束符号为；
				if($ImportProductTransSheetList [0]['other_image_url8'] != Null && $ImportProductTransSheetList [0]['other_image_url8'] != "" && $ImportProductTransSheetList [0]['other_image_url8'] != 'Null'){
					$sqlB->Append("('other_image_url8', '" . $ImportProductTransSheetList [0]['other_image_url8'] . "'); ");
				}else{
					$sqlB->Append("('other_image_url8', NULL); ");
				}

				
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("SELECT `ID` ");
				$sqlB->Append("FROM " . $tempTableName . " AS tempTableName ");
				$sqlB->Append("WHERE tempTableName.Url IS NULL ");
				$sqlB->Append("ORDER BY tempTableName.`ID` ");
				$sqlB->Append("Limit 1 ");
				$sqlB->Append(";");
				$List = $Model->query($sqlB->ToString());
				$Model->execute("DROP TABLE IF EXISTS " . $tempTableName. "; ");
			
				if($List[0]['ID'] != ""){
					$sqlB = new StringBuilder();
					$sqlB->Append("UPDATE ImportProductTransSheet ");
					$sqlB->Append("SET ");
					$sqlB->Append("   ImportProductTransSheet." . $List[0]['ID'] . " = " . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
					$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
					$sqlB->Append("AND ImportProductTransSheet.color_name = ".FData($ColorName, "Text") . " ");
					$sqlB->Append(";");
					$Model->execute($sqlB->ToString());

					$sqlB = new StringBuilder();
					$sqlB->Append("UPDATE ImportProductSheet ");
					$sqlB->Append("SET ");
					$sqlB->Append("   ImportProductSheet." . $List[0]['ID'] . " = " . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
					$sqlB->Append("WHERE ImportProductSheet.ID = ".$ImportProductSheetID . " ");
					$sqlB->Append("AND ImportProductSheet." . $List[0]['ID'] . " IS NULL ");
					$sqlB->Append(";");
					$Model->execute($sqlB->ToString());

					$arr = array
					(
						'URLID'=> $List[0]['ID'],
						'URL'=>'/Uploads/'.$_SESSION['UserID']. "/s_" . $info[0]['savename']
					);

					$jsonencode = json_encode($arr);
				}
			}
		}
		echo $jsonencode;
	}


	Public function GetSelectVale($Model, $TableName, $Where = ""){
	 
	 	$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  " . $TableName . " ");
		if($Where != ""){
			$sqlB->Append(" WHERE " . $Where . " ");
		}
		$sqlB->Append(";");
	
		$TableList = $Model->query($sqlB->ToString());
		return $TableList;
	 }


	Public function Add(){
		$Model = new Model();
		
		$ProductSize = null;
		$ProductColor = null;
		//$ColorMap = null;

		$ProductSize = array_filter($_POST['ProductSize']);
		$ProductColor = array_filter($_POST['ProductColor']);
		//$ColorMap = array_filter($_POST['color_map']);

		$ProductSubCount = Count($ProductSize) * Count($ProductColor);
		$ProductSubTypeID = $_POST['ProductSubTypeID'];
		$SecondProductSubTypeID = $_POST['SecondProductSubTypeID'];
		$ThirdProductSubTypeID = $_POST['ThirdProductSubTypeID'];
		$FourthProductSubTypeID = $_POST['FourthProductSubTypeID'];
		$FifthProductSubTypeID = $_POST['FifthProductSubTypeID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubType ");
		$sqlB->Append("FROM  ProductSubTypes ");
		$sqlB->Append("WHERE Code = " . $ProductSubTypeID . ";");
	
		$ProductSubTypeList = $Model->query($sqlB->ToString());

		$product_subtype = $ProductSubTypeList[0]['ProductSubType'];

		$ProductSubTypeArr = array($ProductSubTypeID, $SecondProductSubTypeID, $ThirdProductSubTypeID, $FourthProductSubTypeID, $FifthProductSubTypeID);
		$recommended_browse_nodes = array_pop(array_filter($ProductSubTypeArr));


		$brand_name_en = trim($_POST['brand_name_en']);
		$brand_name_zh = trim($_POST['brand_name_zh']);
		$temp_item_name = trim($_POST['item_name']);
		$manufacturer = trim($_POST['manufacturer']);
		$country_of_origin = trim($_POST['country_of_origin']);
		$sole_material = trim($_POST['sole_material']);
		//英文品牌+空格+汉语品牌+空格+具体名称的格式填写，比如，Nike 耐克 复古airmax跑鞋。
		//另，在有变体的商品中子商品名称是，此处应该是品牌+商品名称加上卖家在变体属性中添加的尺码和颜色。例如父商品名称是Nike 耐克 复古airmax跑鞋，
		//那他的子商品应该是Nike 耐克 复古airmax跑鞋 黑色 36.

		$item_name = "";
		$brand_name = "";

		if($brand_name_en != ""){
			$item_name .= $brand_name_en .  " ";
			$brand_name .= $brand_name_en .  " ";
		}

		if($brand_name_zh != ""){
			$item_name .= $brand_name_zh .  " ";
			$brand_name .= $brand_name_zh .  " ";
		}

		if($temp_item_name != ""){
			$item_name .= $temp_item_name .  " ";
		}

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportSheet.ImportSheetID ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("WHERE DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d')= '". date("Y-m-d", time()). "'");
		$sqlB->Append("AND ImportSheet.UserID = ".$_SESSION['UserID'] . " ");
		$sqlB->Append("Limit 1;");

		$ImportSheetList = $Model->query($sqlB->ToString());


		$sqlB = new StringBuilder();
		$sqlB->Append("INSERT INTO ");
		$sqlB->Append("   ImportProductSheet ( ");
		$sqlB->Append("   ImportSheetID ");
		$sqlB->Append(" , item_name ");
		$sqlB->Append(" , brand_name ");
		$sqlB->Append(" , brand_name_en ");
		$sqlB->Append(" , brand_name_zh ");
		$sqlB->Append(" , product_subtype ");
		$sqlB->Append(" , manufacturer ");
		$sqlB->Append(" , recommended_browse_nodes ");

		$sqlB->Append(" , country_of_origin ");
		$sqlB->Append(" , sole_material ");
		$sqlB->Append(" , currency ");
		$sqlB->Append(" , update_delete ");
		$sqlB->Append(" , parent_child ");
		$sqlB->Append(" , variation_theme ");
		$sqlB->Append(" , TimeUpdated ");
		$sqlB->Append(" , UpdatedBy ");
		$sqlB->Append(" , TimeCreated ");
		$sqlB->Append(" , CreatedBy ");
		$sqlB->Append("   ) ");
		$sqlB->Append("SELECT ");
		$sqlB->Append("   " . $ImportSheetList[0]['ImportSheetID'] . " AS ImportSheetID ");
		$sqlB->Append(" , " . FData($item_name, "Text") . " AS item_name ");
		$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
		$sqlB->Append(" , " . FData($brand_name_en, "Text") . " AS brand_name_en ");
		$sqlB->Append(" , " . FData($brand_name_zh, "Text") . " AS brand_name_zh ");
		$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
		$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
		$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes ");

		$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
		$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");
		$sqlB->Append(" , '人民币' AS currency ");
		$sqlB->Append(" , '更新' AS update_delete ");
		$sqlB->Append(" , '父商品' AS parent_child ");

		if(Count($ProductSize)>0 && Count($ProductColor)>0){
			$sqlB->Append(" , '颜色+尺码' AS variation_theme ");
		}
		elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
			$sqlB->Append(" , '颜色' AS variation_theme ");
		}
		elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
			$sqlB->Append(" , '尺码' AS variation_theme ");
		}
		
		$sqlB->Append(" , NOW() AS TimeUpdated ");
		$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
		$sqlB->Append(" , NOW() AS TimeCreated ");
		$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
		$sqlB->Append(";");

		$ImportProductSheetIDStr = $Model->execute($sqlB->ToString());
		$ImportProductSheetIDArr = explode("^", $ImportProductSheetIDStr);

		if($ImportProductSheetIDArr[0]){
		
			for ($i =0; $i< Count($ProductSize); $i++ )
			{
				for ($j =0; $j< Count($ProductColor); $j++ )
				{
					
					$item_name_sub = $item_name;
					if(trim($ProductColor[$j]) != ""){
						$item_name_sub .= trim($ProductColor[$j]) .  " ";
					}

					if(trim($ProductSize[$i]) != ""){
						$item_name_sub .= trim($ProductSize[$i]) .  " ";
					}
					
					$sqlB = new StringBuilder();
					$sqlB->Append("INSERT INTO ");
					$sqlB->Append("   ImportProductTransSheet ( ");
					$sqlB->Append("   ParentID ");
					$sqlB->Append(" , item_name ");
					$sqlB->Append(" , brand_name ");
					$sqlB->Append(" , color_name ");
					//$sqlB->Append(" , color_map ");
					$sqlB->Append(" , size_name ");
					$sqlB->Append(" , product_subtype ");
					$sqlB->Append(" , manufacturer ");
					
					$sqlB->Append(" , recommended_browse_nodes ");
					
					$sqlB->Append(" , country_of_origin ");
					$sqlB->Append(" , sole_material ");

					$sqlB->Append(" , currency ");
					$sqlB->Append(" , update_delete ");
					$sqlB->Append(" , parent_child ");
					$sqlB->Append(" , variation_theme ");
					$sqlB->Append(" , relationship_type ");
					$sqlB->Append(" , TimeUpdated ");
					$sqlB->Append(" , UpdatedBy ");
					$sqlB->Append(" , TimeCreated ");
					$sqlB->Append(" , CreatedBy ");
					$sqlB->Append("   ) ");
					$sqlB->Append("SELECT ");
					$sqlB->Append("   " . $ImportProductSheetIDArr[1] . " AS ParentID ");
					$sqlB->Append(" , " . FData($item_name_sub, "Text") . " AS item_name ");
					$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
					$sqlB->Append(" , " . FData(trim($ProductColor[$j]), "Text") . " AS color_name ");
					//$sqlB->Append(" , '" . trim($ColorMap[$j]) . "' AS color_map ");
					$sqlB->Append(" , " . FData(trim($ProductSize[$i]), "Text") . " AS size_name ");
					$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
					$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
					$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes ");
					$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
					$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");

					$sqlB->Append(" , '人民币' AS currency ");
					$sqlB->Append(" , '更新' AS update_delete ");
					$sqlB->Append(" , '子商品' AS parent_child ");

					if(Count($ProductSize)>0 && Count($ProductColor)>0){
						$sqlB->Append(" , '颜色+尺码' AS variation_theme ");
					}
					elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
						$sqlB->Append(" , '颜色' AS variation_theme ");
					}
					elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
						$sqlB->Append(" , '尺码' AS variation_theme ");
					}
					
					$sqlB->Append(" , '变体' AS relationship_type ");

					$sqlB->Append(" , NOW() AS TimeUpdated ");
					$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
					$sqlB->Append(" , NOW() AS TimeCreated ");
					$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
					$sqlB->Append(";");
					$Model->execute($sqlB->ToString());
				}
			}
		}


		if($ImportProductSheetIDArr[1]){
			$this->redirect("/ImportSheet/Edit2/ImportProductSheetID/". $ImportProductSheetIDArr[1] . "");
		}
		else{
			$this->error('添加失败！', "/ImportSheet/Edit2/");
		}
		
	}

	Public function ReEdit(){

		$ImportProductSheetID = $_POST['ImportProductSheetID'];
		if($ImportProductSheetID == ""){
			return;
		}

		$Model = new Model();

		$ProductSize = null;
		$ProductColor = null;
		//$ColorMap = null;

		$ProductSize = array_filter($_POST['ProductSize']);
		$ProductColor = array_filter($_POST['ProductColor']);
		//$ColorMap = array_filter($_POST['color_map']);

		$ProductSubCount = Count($ProductSize) * Count($ProductColor);
		$ProductSubTypeID = $_POST['ProductSubTypeID'];
		$SecondProductSubTypeID = $_POST['SecondProductSubTypeID'];
		$ThirdProductSubTypeID = $_POST['ThirdProductSubTypeID'];
		$FourthProductSubTypeID = $_POST['FourthProductSubTypeID'];
		$FifthProductSubTypeID = $_POST['FifthProductSubTypeID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubType ");
		$sqlB->Append("FROM  ProductSubTypes ");
		$sqlB->Append("WHERE Code = " . $ProductSubTypeID . ";");
	
		$ProductSubTypeList = $Model->query($sqlB->ToString());

		$product_subtype = $ProductSubTypeList[0]['ProductSubType'];

		$ProductSubTypeArr = array($ProductSubTypeID, $SecondProductSubTypeID, $ThirdProductSubTypeID, $FourthProductSubTypeID, $FifthProductSubTypeID);
		$recommended_browse_nodes = array_pop(array_filter($ProductSubTypeArr));

		$brand_name_en = trim($_POST['brand_name_en']);
		$brand_name_zh = trim($_POST['brand_name_zh']);
		$temp_item_name = trim($_POST['item_name']);
		$manufacturer = trim($_POST['manufacturer']);
		$country_of_origin = trim($_POST['country_of_origin']);
		$sole_material = trim($_POST['sole_material']);
		//英文品牌+空格+汉语品牌+空格+具体名称的格式填写，比如，Nike 耐克 复古airmax跑鞋。
		//另，在有变体的商品中子商品名称是，此处应该是品牌+商品名称加上卖家在变体属性中添加的尺码和颜色。例如父商品名称是Nike 耐克 复古airmax跑鞋，
		//那他的子商品应该是Nike 耐克 复古airmax跑鞋 黑色 36.

		$item_name = "";
		$brand_name = "";

		if($brand_name_en != ""){
			$item_name .= $brand_name_en .  " ";
			$brand_name .= $brand_name_en .  " ";
		}

		if($brand_name_zh != ""){
			$item_name .= $brand_name_zh .  " ";
			$brand_name .= $brand_name_zh .  " ";
		}

		if($temp_item_name != ""){
			$item_name .= $temp_item_name .  " ";
		}

		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE  ");
		$sqlB->Append("   ImportProductSheet  ");
		$sqlB->Append(" SET ");
		$sqlB->Append("   item_name = " .  FData($item_name, "Text") . " ");
		$sqlB->Append(" , brand_name = " . FData($brand_name, "Text") . " ");
		$sqlB->Append(" , brand_name_en = " . FData($brand_name_en, "Text") . " ");
		$sqlB->Append(" , brand_name_zh = " . FData($brand_name_zh, "Text") . " ");
		$sqlB->Append(" , product_subtype = " . FData($product_subtype, "Text") . " ");
		$sqlB->Append(" , manufacturer = " . FData($manufacturer, "Text") . " ");
		$sqlB->Append(" , recommended_browse_nodes = " . FData($recommended_browse_nodes, "Text") . " ");

		$sqlB->Append(" , country_of_origin =  ". FData($country_of_origin, "Text"). " ");
		$sqlB->Append(" , sole_material = ". FData($sole_material, "Text"). " ");
		$sqlB->Append(" , variation_theme");
		
		if(Count($ProductSize)>0 && Count($ProductColor)>0){
			$sqlB->Append(" = '颜色+尺码' ");
		}
		elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
			$sqlB->Append(" = '颜色' ");
		}
		elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
			$sqlB->Append(" = '尺码' ");
		}
		$sqlB->Append(" , TimeUpdated =  NOW() ");
		$sqlB->Append(" , UpdatedBy = '". $_SESSION['UserName'] . "' ");
		$sqlB->Append("WHERE ID = " . $ImportProductSheetID);
		$sqlB->Append(";");
		
		$ImportProductSheetIDStr = $Model->execute($sqlB->ToString());
		$ImportProductSheetIDArr = explode("^", $ImportProductSheetIDStr);

		if($ImportProductSheetIDArr[0]){
		
			for ($i =0; $i< Count($ProductSize); $i++ )
			{
				for ($j =0; $j< Count($ProductColor); $j++ )
				{
					
					$item_name_sub = $item_name;
					if(trim($ProductColor[$j]) != ""){
						$item_name_sub .= trim($ProductColor[$j]) .  " ";
					}

					if(trim($ProductSize[$i]) != ""){
						$item_name_sub .= trim($ProductSize[$i]) .  " ";
					}
							
					$sqlB = new StringBuilder();
					$sqlB->Append("SELECT ");
					$sqlB->Append("   ImportProductTransSheet.ID ");
					$sqlB->Append("FROM  ImportProductTransSheet ");
					$sqlB->Append("WHERE ParentID = " . $ImportProductSheetID. " ");
					$sqlB->Append("AND color_name = '" . trim($ProductColor[$j]) . "' ");
					$sqlB->Append("AND size_name = '" . trim($ProductSize[$i])  . "' ");
					$sqlB->Append("Limit 1;");

					$ImportProductTransSheet = $Model->query($sqlB->ToString());

					if(Count($ImportProductTransSheet) == 1 ){
						$sqlB = new StringBuilder();

						$sqlB->Append("UPDATE  ");
						$sqlB->Append("   ImportProductTransSheet  ");
						$sqlB->Append(" SET ");
						$sqlB->Append("   item_name = " . FData($item_name_sub, "Text") . " ");
						$sqlB->Append(" , brand_name = " . FData($brand_name, "Text") . " ");
						$sqlB->Append(" , color_name = " . FData(trim($ProductColor[$j]), "Text") . " ");
						//$sqlB->Append(" , color_map = " . trim($ColorMap[$j]) . " ");
						$sqlB->Append(" , size_name = " . FData(trim($ProductSize[$i]), "Text") . " ");
						$sqlB->Append(" , product_subtype = " . FData($product_subtype, "Text") . " ");
						$sqlB->Append(" , manufacturer = " . FData($manufacturer, "Text") . " ");
						$sqlB->Append(" , recommended_browse_nodes = '" . FData($recommended_browse_nodes, "Text") . " ");
						$sqlB->Append(" , country_of_origin =  " . FData($country_of_origin, "Text"). " ");
						$sqlB->Append(" , sole_material = " . FData($sole_material, "Text") . " ");
						$sqlB->Append(" , variation_theme ");
						
						if(Count($ProductSize)>0 && Count($ProductColor)>0){
							$sqlB->Append(" = '颜色+尺码' ");
						}
						elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
							$sqlB->Append(" = '颜色' ");
						}
						elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
							$sqlB->Append(" = '尺码' ");
						}
						$sqlB->Append(" , TimeUpdated =  NOW() ");
						$sqlB->Append(" , UpdatedBy = '". $_SESSION['UserName'] . "' ");
						$sqlB->Append("WHERE ParentID = " . $ImportProductSheetID. " ");
						$sqlB->Append("AND color_name = '" .trim($ProductColor[$j]) . "' ");
						$sqlB->Append("AND size_name = '" . trim($ProductSize[$i])  . "' ");
						$sqlB->Append(";");
						$Model->execute($sqlB->ToString());	
					}
					else{
					
						$sqlB = new StringBuilder();

						$sqlB->Append("INSERT INTO ");
						$sqlB->Append("   ImportProductTransSheet ( ");
						$sqlB->Append("   ParentID ");
						$sqlB->Append(" , item_name ");
						$sqlB->Append(" , brand_name ");
						$sqlB->Append(" , color_name ");
						$sqlB->Append(" , size_name ");
						$sqlB->Append(" , product_subtype ");
						$sqlB->Append(" , manufacturer ");
						
						$sqlB->Append(" , recommended_browse_nodes ");
						$sqlB->Append(" , country_of_origin ");
						$sqlB->Append(" , sole_material ");

						$sqlB->Append(" , currency ");
						$sqlB->Append(" , update_delete ");
						$sqlB->Append(" , parent_child ");
						$sqlB->Append(" , variation_theme ");
						$sqlB->Append(" , relationship_type ");
						$sqlB->Append(" , TimeUpdated ");
						$sqlB->Append(" , UpdatedBy ");
						$sqlB->Append(" , TimeCreated ");
						$sqlB->Append(" , CreatedBy ");
						$sqlB->Append("   ) ");
						$sqlB->Append("SELECT ");
						$sqlB->Append("   " . $ImportProductSheetID . " AS ParentID ");
						$sqlB->Append(" , " . FData($item_name_sub, "Text") . " AS item_name ");
						$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
						$sqlB->Append(" , " . FData(trim($ProductColor[$j]), "Text") . " AS color_name ");
						$sqlB->Append(" , " . FData(trim($ProductSize[$i]), "Text") . " AS size_name ");
						$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
						$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
						
						$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes ");
						$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
						$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");

						$sqlB->Append(" , '人民币' AS currency ");
						$sqlB->Append(" , '更新' AS update_delete ");
						$sqlB->Append(" , '子商品' AS parent_child ");

						if(Count($ProductSize)>0 && Count($ProductColor)>0){
							$sqlB->Append(" , '颜色+尺码' AS variation_theme ");
						}
						elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
							$sqlB->Append(" , '颜色' AS variation_theme ");
						}
						elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
							$sqlB->Append(" , '尺码' AS variation_theme ");
						}
						
						$sqlB->Append(" , '变体' AS relationship_type ");

						$sqlB->Append(" , NOW() AS TimeUpdated ");
						$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
						$sqlB->Append(" , NOW() AS TimeCreated ");
						$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
						$sqlB->Append(";");

						$Model->execute($sqlB->ToString());
					}
				}
			}
		}

		if($ImportProductSheetID){
			$this->redirect("/ImportSheet/Edit2/ImportProductSheetID/". $ImportProductSheetID . "");
		}
		else{
			$this->error('编辑失败！', "/ImportSheet/Edit2/");
		}
	}

	function Edit2(){
	
		$Model = new Model();
		$ImportProductSheetID = $_GET['ImportProductSheetID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  ImportProductSheet ");
		$sqlB->Append("WHERE ImportProductSheet.ID = " . $ImportProductSheetID . " ");
		$sqlB->Append("LIMIT 1");
		$sqlB->Append(";");
		$ImportProductSheetList = $Model->query($sqlB->ToString());
		$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_en'], "", $ImportProductSheetList[0]['item_name']));
		$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_zh'], "", $ImportProductSheetList[0]['item_name']));


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubTypes.ProductSubType ");
		$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
		$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
		$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
		$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
		$sqlB->Append("FROM ProductSubTypes ");
		$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");
		$sqlB->Append("WHERE ");
		$sqlB->Append("ProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR SecondProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR FourthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR FifthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("LIMIT 1 ");
		$sqlB->Append(";");

		$ProductSubTypesList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.size_name ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("GROUP BY ImportProductTransSheet.size_name ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ProductSizeList = $Model->query($sqlB->ToString());


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.color_name ");
		//$sqlB->Append(" , ImportProductTransSheet.color_map ");
		$sqlB->Append(" , ImportProductTransSheet.ID ");
		$sqlB->Append(" , ImportProductTransSheet.main_image_url ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url1 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url2 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url3 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url4 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url5 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url6 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url7 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url8 ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("GROUP BY ImportProductTransSheet.color_name ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ProductColorList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.ID ");
		$sqlB->Append(" , ImportProductTransSheet.color_name ");
		$sqlB->Append(" , ImportProductTransSheet.size_name ");
		$sqlB->Append(" , ImportProductTransSheet.item_sku ");
		$sqlB->Append(" , ImportProductTransSheet.standard_price ");
		$sqlB->Append(" , ImportProductTransSheet.sale_price ");
		$sqlB->Append(" , ImportProductTransSheet.sale_from_date ");
		$sqlB->Append(" , ImportProductTransSheet.sale_end_date ");
		$sqlB->Append(" , ImportProductTransSheet.quantity ");
		$sqlB->Append(" , ImportProductTransSheet.fulfillment_latency ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ImportProductTransSheetList = $Model->query($sqlB->ToString());
		
		$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes");

		
		$this->assign('ProductSubTypesList',$ProductSubTypesList);
		$this->assign('ImportProductSheetList',$ImportProductSheetList);
		$this->assign('ProductSizeList',$ProductSizeList);
		$this->assign('ProductColorList',$ProductColorList);
		$this->assign('ImportProductTransSheetList',$ImportProductTransSheetList);
		
		$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->display("UpdateImportSheet");
	}

	function View(){
	
		$Model = new Model();
		$ImportProductSheetID = $_GET['ImportProductSheetID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  ImportProductSheet ");
		$sqlB->Append("WHERE ImportProductSheet.ID = " . $ImportProductSheetID . " ");
		$sqlB->Append("LIMIT 1");
		$sqlB->Append(";");
		$ImportProductSheetList = $Model->query($sqlB->ToString());
		
		$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_en'], "", $ImportProductSheetList[0]['item_name']));
		$ImportProductSheetList[0]['item_name'] = trim(str_replace($ImportProductSheetList[0]['brand_name_zh'], "", $ImportProductSheetList[0]['item_name']));


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubTypes.ProductSubType ");
		$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
		$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
		$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
		$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
		$sqlB->Append("FROM ProductSubTypes ");
		$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");
		$sqlB->Append("WHERE ");
		$sqlB->Append("ProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR SecondProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR FourthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("OR FifthProductSubTypes.Code = " . $ImportProductSheetList[0]['recommended_browse_nodes'] . " ");
		$sqlB->Append("LIMIT 1 ");
		$sqlB->Append(";");
		$ProductSubTypesList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.size_name ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("GROUP BY ImportProductTransSheet.size_name ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ProductSizeList = $Model->query($sqlB->ToString());


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.color_name ");
		//$sqlB->Append(" , ImportProductTransSheet.color_map ");
		$sqlB->Append(" , ImportProductTransSheet.main_image_url ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url1 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url2 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url3 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url4 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url5 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url6 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url7 ");
		$sqlB->Append(" , ImportProductTransSheet.other_image_url8 ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("GROUP BY ImportProductTransSheet.color_name ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ProductColorList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.ID ");
		$sqlB->Append(" , ImportProductTransSheet.color_name ");
		$sqlB->Append(" , ImportProductTransSheet.size_name ");
		$sqlB->Append(" , ImportProductTransSheet.item_sku ");
		$sqlB->Append(" , ImportProductTransSheet.standard_price ");
		$sqlB->Append(" , ImportProductTransSheet.sale_price ");
		$sqlB->Append(" , ImportProductTransSheet.sale_from_date ");
		$sqlB->Append(" , ImportProductTransSheet.sale_end_date ");
		$sqlB->Append(" , ImportProductTransSheet.quantity ");
		$sqlB->Append(" , ImportProductTransSheet.fulfillment_latency ");
		
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID ;");
		$ImportProductTransSheetList = $Model->query($sqlB->ToString());
		
		//$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes");

		
		$this->assign('ProductSubTypesList',$ProductSubTypesList);
		$this->assign('ImportProductSheetList',$ImportProductSheetList);
		$this->assign('ProductSizeList',$ProductSizeList);
		$this->assign('ProductColorList',$ProductColorList);
		$this->assign('ImportProductTransSheetList',$ImportProductTransSheetList);
		
		//$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->display("ViewImportSheet");
	}

	function Update(){
		$Model = new Model();
		$ReturnUrl = $_GET['ReturnAction'];
		$ImportProductSheetID = $_POST['ImportProductSheetID'];
		//更新ImportProductSheet表 ：父商品表
		$product_description = $_POST['product_description'];
		$product_description = str_replace(Chr(13),"", $product_description);
		$product_description = str_replace(Chr(10),"<br />", $product_description);
		
		$safety_warning = $_POST['safety_warning'];
		$safety_warning = str_replace(Chr(13),"", $safety_warning);
		$safety_warning = str_replace(Chr(10),"<br />", $safety_warning);

		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE ImportProductSheet ");
		$sqlB->Append("SET ");
		$sqlB->Append("   item_sku =" . FData($_POST['item_sku'][0], "Text"));
		$sqlB->Append(" , product_description = '" . $product_description . "' ");
		$sqlB->Append(" , safety_warning = '" . $safety_warning . "' ");
		//$sqlB->Append(" , country_of_origin =  ". FData($_POST['country_of_origin'], "Text"));
		//$sqlB->Append(" , sole_material = ". FData($_POST['sole_material'], "Text"));
        $sqlB->Append(" , country_string = ". FData($_POST['country_string'], "Text"));
		$sqlB->Append(" , department_name = ". FData($_POST['department_name'], "Text"));
		$sqlB->Append(" , closure_type = ". FData($_POST['closure_type'], "Text"));
		$sqlB->Append(" , heel_type = ". FData($_POST['heel_type'], "Text"));
		$sqlB->Append(" , lifestyle1 = ". FData($_POST['lifestyle1'], "Text"));
		$sqlB->Append(" , special_features1 = ". FData($_POST['special_features1'], "Text"));
		$sqlB->Append(" , special_features2 = ". FData($_POST['special_features2'], "Text"));
		$sqlB->Append(" , special_features3 = ". FData($_POST['special_features3'], "Text"));
		$sqlB->Append(" , height_map = ". FData($_POST['height_map'], "Text"));
		$sqlB->Append(" , lining_description = ". FData($_POST['lining_description'], "Text"));
		$sqlB->Append(" , toe_style = ". FData($_POST['toe_style'], "Text"));
		$sqlB->Append(" , water_resistance_level = ". FData($_POST['water_resistance_level'], "Text"));
		$sqlB->Append(" , seasons = ". FData($_POST['seasons'], "Text"));
		$sqlB->Append(" , pattern_type = ". FData($_POST['pattern_type'], "Text"));
		$sqlB->Append(" , rise_style = ". FData($_POST['rise_style'], "Text"));
		$sqlB->Append(" , material_type1 = ". FData($_POST['material_type1'], "Text"));
		$sqlB->Append(" , fulfillment_latency = Null ");
		$sqlB->Append(" , platform_height = ". FData($_POST['platform_height'], "Text"). " ");

		$sqlB->Append(" , style_keywords1 = " . FData($_POST['style_keywords1'], "Text") . " ");
		$sqlB->Append(" , style_keywords2 = " . FData($_POST['style_keywords2'], "Text") . " ");
		$sqlB->Append(" , style_keywords3 = " . FData($_POST['style_keywords3'], "Text") . " ");
		$sqlB->Append(" , style_keywords4 = " . FData($_POST['style_keywords4'], "Text") . " ");
		$sqlB->Append(" , style_keywords5 = " . FData($_POST['style_keywords5'], "Text") . " ");

		$sqlB->Append(" , generic_keywords1 = " . FData($_POST['generic_keywords1'], "Text") . " ");
		$sqlB->Append(" , generic_keywords2 = " . FData($_POST['generic_keywords2'], "Text") . " ");
		$sqlB->Append(" , generic_keywords3 = " . FData($_POST['generic_keywords3'], "Text") . " ");
		$sqlB->Append(" , generic_keywords4 = " . FData($_POST['generic_keywords4'], "Text") . " ");
		$sqlB->Append(" , generic_keywords5 = " . FData($_POST['generic_keywords5'], "Text") . " ");
		$sqlB->Append("WHERE ImportProductSheet.ID = ".$ImportProductSheetID . " ");

		$Model->execute($sqlB->ToString());
		
	

		//搜索ImportProductTransSheet表 ：子商品表，用来更新生成SKU以及价格库存等。
		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportProductTransSheet.ID ");
		$sqlB->Append("FROM ImportProductTransSheet ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
		$sqlB->Append("ORDER BY ImportProductTransSheet.ID;");
		$ImportProductTransSheetList = $Model->query($sqlB->ToString());

		//更新ImportProductTransSheet表 SKU以及价格库存 ：子商品表。
		
		for ($i = 0; $i < count($ImportProductTransSheetList); $i++)
		{		
			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE ImportProductTransSheet ");
			$sqlB->Append("SET ");
			$sqlB->Append("   item_sku =" . FData($_POST['item_sku'][$ImportProductTransSheetList[$i]['ID']], "Text"));
			$sqlB->Append(" , standard_price =" . FData($_POST['standard_price'][$ImportProductTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_price =" . FData($_POST['sale_price'][$ImportProductTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_from_date = " . FData($_POST['sale_from_date'][$ImportProductTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_end_date = " . FData($_POST['sale_end_date'][$ImportProductTransSheetList[$i]['ID']], "Text" ));
			$sqlB->Append(" , quantity =" . FData($_POST['quantity'][$ImportProductTransSheetList[$i]['ID']], "Text"). " " );
			$sqlB->Append("WHERE ImportProductTransSheet.ID = ".$ImportProductTransSheetList[$i]['ID'] . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
		}

		//更新ImportProductTransSheet表 其他的一些字段 ：子商品表。
		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE ImportProductTransSheet ");
		$sqlB->Append("SET ");
		$sqlB->Append("   product_description = '" . $product_description  . "' ");
		$sqlB->Append(" , safety_warning = '" . $safety_warning  . "' ");
		//$sqlB->Append(" , country_of_origin =  " . FData($_POST['country_of_origin'], "Text"));
        $sqlB->Append(" , country_string = " . FData($_POST['country_string'], "Text"));
		//$sqlB->Append(" , sole_material = " . FData($_POST['sole_material'], "Text"));
		$sqlB->Append(" , department_name = " . FData($_POST['department_name'], "Text"));
		$sqlB->Append(" , closure_type = " . FData($_POST['closure_type'], "Text"));
		$sqlB->Append(" , heel_type = " . FData($_POST['heel_type'], "Text"));
		$sqlB->Append(" , lifestyle1 = " . FData($_POST['lifestyle1'], "Text"));
		$sqlB->Append(" , special_features1 = " . FData($_POST['special_features1'], "Text"));
		$sqlB->Append(" , special_features2 = " . FData($_POST['special_features2'], "Text"));
		$sqlB->Append(" , special_features3 = " . FData($_POST['special_features3'], "Text"));
		$sqlB->Append(" , height_map = " . FData($_POST['height_map'], "Text"));
		$sqlB->Append(" , lining_description = " . FData($_POST['lining_description'], "Text"));
		$sqlB->Append(" , toe_style = " . FData($_POST['toe_style'], "Text"));
		$sqlB->Append(" , water_resistance_level = " . FData($_POST['water_resistance_level'], "Text"));
		$sqlB->Append(" , seasons = " . FData($_POST['seasons'], "Text"));
		$sqlB->Append(" , pattern_type = " . FData($_POST['pattern_type'], "Text"));
		$sqlB->Append(" , rise_style = " . FData($_POST['rise_style'], "Text"));
		$sqlB->Append(" , material_type1 = " . FData($_POST['material_type1'], "Text"));
		$sqlB->Append(" , fulfillment_latency = ". FData($_POST['fulfillment_latency'], "Text") . " ");
		$sqlB->Append(" , platform_height = ". FData($_POST['platform_height'], "Text"). " ");


		$sqlB->Append(" , parent_sku = ". FData($_POST['item_sku'][0], "Text") . " ");
		$sqlB->Append(" , style_keywords1 = " . FData($_POST['style_keywords1'], "Text") . " ");
		$sqlB->Append(" , style_keywords2 = " . FData($_POST['style_keywords2'], "Text") . " ");
		$sqlB->Append(" , style_keywords3 = " . FData($_POST['style_keywords3'], "Text") . " ");
		$sqlB->Append(" , style_keywords4 = " . FData($_POST['style_keywords4'], "Text") . " ");
		$sqlB->Append(" , style_keywords5 = " . FData($_POST['style_keywords5'], "Text") . " ");

		$sqlB->Append(" , generic_keywords1 = " . FData($_POST['generic_keywords1'], "Text") . " ");
		$sqlB->Append(" , generic_keywords2 = " . FData($_POST['generic_keywords2'], "Text") . " ");
		$sqlB->Append(" , generic_keywords3 = " . FData($_POST['generic_keywords3'], "Text") . " ");
		$sqlB->Append(" , generic_keywords4 = " . FData($_POST['generic_keywords4'], "Text") . " ");
		$sqlB->Append(" , generic_keywords5 = " . FData($_POST['generic_keywords5'], "Text") . " ");
		$sqlB->Append("WHERE ImportProductTransSheet.ParentID = " . $ImportProductSheetID . " ");

		$Model->execute($sqlB->ToString());

		if($ReturnUrl != ""){
			$this->redirect("/ImportSheet/Edit");
		}
		else{
			$this->redirect("/ImportSheet/ViewParentsProduct");
		}
	}


	function CopyImportProductSheet(){
		$ImportProductSheetID = $_GET['ImportProductSheetID'];
		
		if($ImportProductSheetID != ""){

			$Model = new Model();
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `TableFields`.`Field` ");
			$sqlB->Append("FROM `TableFields` ");
			$sqlB->Append("WHERE `TableFields`.`Table` = 'ImportProductSheet' ");
			$sqlB->Append("ORDER BY Sequence ");
			$sqlB->Append(";");
			$TableFieldsList = $Model->query($sqlB->ToString());


			$Model = new Model();
			$sqlB = new StringBuilder();

			$sqlB->Append("INSERT INTO  ImportProductSheet ");
			$sqlB->Append("(");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}

					if($TableFieldsList[$i]['Field'] == "brand_name"){
					
						$sqlB->Append(" brand_name_en , ");
						$sqlB->Append(" brand_name_zh , ");
					}
				}
			}
			$sqlB->Append(")");
			$sqlB->Append("SELECT ");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					if($TableFieldsList[$i]['Field'] == "TimeUpdated" ||$TableFieldsList[$i]['Field'] == "TimeCreated"){
						$sqlB->Append(" NOW() AS " . $TableFieldsList[$i]['Field']);
					}
					elseif($TableFieldsList[$i]['Field'] == "item_name"){
						$sqlB->Append(" CONCAT(" . $TableFieldsList[$i]['Field'] . " , '_副本') AS " . $TableFieldsList[$i]['Field']. " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "main_image_url" || $TableFieldsList[$i]['Field'] == "other_image_url1"|| $TableFieldsList[$i]['Field'] == "other_image_url2"|| $TableFieldsList[$i]['Field'] == "other_image_url3"|| $TableFieldsList[$i]['Field'] == "other_image_url4"|| $TableFieldsList[$i]['Field'] == "other_image_url5"|| $TableFieldsList[$i]['Field'] == "other_image_url6"|| $TableFieldsList[$i]['Field'] == "other_image_url7"|| $TableFieldsList[$i]['Field'] == "other_image_url8"|| $TableFieldsList[$i]['Field'] == "swatch_image_url"){
					
						$sqlB->Append("  Null AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					else{
						$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					}
					
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}

					if($TableFieldsList[$i]['Field'] == "brand_name"){
					
						$sqlB->Append(" brand_name_en , ");
						$sqlB->Append(" brand_name_zh , ");
					}
				}
			}
			$sqlB->Append("FROM ImportProductSheet ");
			$sqlB->Append("WHERE ImportProductSheet.ID = ".$ImportProductSheetID . " ");
			$sqlB->Append("; ");
			
			$ImportProductSheetIDStr = $Model->execute($sqlB->ToString());
			$ImportProductSheetIDArr = explode("^", $ImportProductSheetIDStr);
		
			
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `TableFields`.`Field` ");
			$sqlB->Append("FROM `TableFields` ");
			$sqlB->Append("WHERE `TableFields`.`Table` = 'ImportProductTransSheet' ");
			$sqlB->Append("ORDER BY Sequence ");
			$sqlB->Append(";");
			$TableFieldsList = $Model->query($sqlB->ToString());


			$Model = new Model();
			$sqlB = new StringBuilder();

			$sqlB->Append("INSERT INTO  ImportProductTransSheet ");
			$sqlB->Append("(");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}
				}
			}
			$sqlB->Append(")");
			$sqlB->Append("SELECT ");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					if($TableFieldsList[$i]['Field'] == "ParentID"){
						$sqlB->Append($ImportProductSheetIDArr[1] . " AS " . $TableFieldsList[$i]['Field']);
					}
					elseif($TableFieldsList[$i]['Field']== "TimeUpdated" || $TableFieldsList[$i]['Field'] == "TimeCreated"){
						$sqlB->Append("  NOW() AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "item_name"){
						$sqlB->Append(" CONCAT(" . $TableFieldsList[$i]['Field'] . " , '_副本') AS " . $TableFieldsList[$i]['Field']. " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "main_image_url" || $TableFieldsList[$i]['Field'] == "other_image_url1"|| $TableFieldsList[$i]['Field'] == "other_image_url2"|| $TableFieldsList[$i]['Field'] == "other_image_url3"|| $TableFieldsList[$i]['Field'] == "other_image_url4"|| $TableFieldsList[$i]['Field'] == "other_image_url5"|| $TableFieldsList[$i]['Field'] == "other_image_url6"|| $TableFieldsList[$i]['Field'] == "other_image_url7"|| $TableFieldsList[$i]['Field'] == "other_image_url8"|| $TableFieldsList[$i]['Field'] == "swatch_image_url"){
					
						$sqlB->Append("  Null AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					else{
						$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					}

					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}
				}
			}

			$sqlB->Append("FROM ImportProductTransSheet ");
			$sqlB->Append("WHERE ImportProductTransSheet.ParentID = ".$ImportProductSheetID . " ");
			$sqlB->Append("; ");
			$Model->query($sqlB->ToString());
		}
		
		$this->redirect("/ImportSheet/ViewParentsProduct");
	}


}
